//
//  VisitorLoginView.swift
//  weibo
//
//  Created by apple on 16/2/20.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

//定义代理协议
@objc protocol VisitorLoginViewDelagate: NSObjectProtocol{

        //声明协议方法
    func userWillLogin()
    func userWithRegister()

}


class VisitorLoginView:UIView {
    
    //声明代理对象  用weak
    weak var visitorLoginViewDelagate: VisitorLoginViewDelagate?
    
    //自定义访客视图
    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func registerBtnDidClick(){
    
    //调用代理方法
    visitorLoginViewDelagate?.userWithRegister()
    }
    
    @objc private func loginBtnDidClick(){
        
    visitorLoginViewDelagate?.userWillLogin()
    
    }
    
    func setupInfoWith(tipText: String, imageName:String?){
        tipLable.text = tipText
        if imageName != nil {
            //一定不是首页
            self.cicrcleIcon.image = UIImage(named: imageName!)
            self.largeHouse.hidden = true
            //将cicrcleIcon移到最顶层
            self.bringSubviewToFront(self.cicrcleIcon)
        }else{
            //在首页  做动画
            startAnimation()
        }
        
    }
    
    //设置动画
    private func startAnimation(){
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        //动画时间
        anim.duration = 20
        anim.removedOnCompletion = false
        //基础动画添加到图层
        cicrcleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setupUI(){
        addSubview(cicrcleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginbtn)
        addSubview(registerBtn)
        
        //添加约束
        cicrcleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cicrcleIcon.snp_center)
        }
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cicrcleIcon.snp_centerX)
            make.top.equalTo(cicrcleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginbtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.width.equalTo(100)
            make.height.equalTo(40)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
        }
        //设置注册按钮的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLable.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.height.equalTo(40)
        }
        
        //设置背景图片的约束
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cicrcleIcon.snp_center)
        }
        
        //背景颜色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        loginbtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        
        
    }
    
    //子视图控件
    //使用懒加载获取
    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var cicrcleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示登录文案
    private lazy var tipLable:UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    //登录按钮
    private lazy var loginbtn:UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: themeColor)
    
    
    
    
}
